<template>
 <div class='container'>
  <!-- 外面最大的卡片 -->
  <el-card class="card" v-loading='isLoading'>
    <!-- 头部两个框两个按钮 -->
<el-row>
  <el-col :span="22"><div class="grid-content bg-purple">
    <el-form :inline="true" :model="formInline" size="small"  >
  <el-form-item  class="bqname" label="标签名称">
    <el-input v-model="formInline.directorryName1"></el-input>
  </el-form-item>
  <el-form-item class="ztname" label="状态">
    <el-select v-model="formInline.region" placeholder="请选择">
      <el-option  label="启用" :value="1"></el-option>
      <el-option  label="禁用" :value="0"></el-option>
    </el-select>
  </el-form-item>
  <el-form-item>
     <el-button size="small" @click="resetInput">清除</el-button>
    <el-button size="small" type="primary" @click="sousuo">搜索</el-button>
  </el-form-item>
</el-form>
    </div></el-col>
    <!-- 头部右侧新增按钮 -->
  <el-col :span="2"><div class="grid-content bg-purple-light">
    <el-button size="small" type="success" class="add" icon="el-icon-edit" @click="addEmployeeFn">新增标签</el-button>
    </div></el-col>
</el-row>

  <!-- 提示消息 -->
   <el-alert
    :title="`数据一共 ${counts} 条`"
    type="info"
    show-icon
    :closable="false"
    class="alert">
  </el-alert>

  <!-- 表格区域 -->
    <el-table
      class="ones"
      :data="employeeList"

      >
         <el-table-column
         prop="date"
          label="序号"
          align="left"
          width="100"
          type="index"
          :index="indexMethod">
         </el-table-column>
         <el-table-column
          label="所属学科"
          align="left"
          width="250"
          prop="subjectName">
         </el-table-column>
         <el-table-column
          label="标签名"
          align="left"
          width="240"
          prop="tagName">
         </el-table-column>
         <el-table-column
          label="创建者"
          align="left"
          width="240"
          prop="username">
         </el-table-column>
         <el-table-column
          label="创建日期"
          align="left"
          width="230"
          prop="addDate">
           <template slot-scope="scope">
            {{ formatime(scope.row.addDate)}}
          </template>
         </el-table-column>
         <el-table-column
          label="状态"
          align="left"
          width="200"
          prop="state">
          <template #default="scope">
            {{ scope.row.state === 1 ? '已启用' : '已禁用' }}
          </template>
         </el-table-column>
         <el-table-column
          label="操作"
          align="left"
          width="187"
          >
          <template #default="scope">
            <el-button type="text" @click="editState(scope.row.id,scope.row.state)" >{{ scope.row.state === 1 ? '禁用' : '启用' }}</el-button>
            <el-button type="text" :disabled="scope.row.state === 1" @click="updateApi(scope.row)">修改</el-button>
            <el-button type="text" :disabled="scope.row.state === 1" @click="outApi(scope.row)">删除</el-button>
          </template>
         </el-table-column>

      <!-- <el-table-column
        prop="name"
        label="姓名"
        width="180">
      </el-table-column>
      <el-table-column
        prop="address"
        label="地址">
      </el-table-column> --> -->
    </el-table>

    <!-- 分页组件 -->
      <el-pagination
      class="pagination"
      background
      :counts="counts"
      :current-page="page"
      :page-size="pagesize"
      :page-sizes="[5, 10, 20, 50]"
      layout="prev,pager,next,sizes,jumper"
      :total="counts"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      >
      </el-pagination>
</el-card>
<tagsAdd :showDialog.sync="showDialog" @add-success="tagS" :tagsss="tagsss"/> <!--@add-success="tagS"-->

 </div>
</template>

<script>
import { list, remove, changeState } from '@/api/hmmm/tags'
import tagsAdd from '../components/tags-add.vue'
import { parseTime } from '@/utils'
// import tagsAdd from '../components/tags-add.vue'
export default {
  name: 'Tags',
  data () {
    return {
      page: 1, // 当前页
      pagesize: 10, // 一页五条
      pages: 0, // 总页数
      employeeList: [],
      counts: 0, // 总条数4
      directoryName: '',
      state: null,
      // currentPage1: 5,
      // currentPage2: 5,
      // currentPage3: 5,
      // currentPage4: 4,
      isLoading: false,
      formInline: {
        user: '',
        region: '',
        directorryName1: ''
      },
      // tableData: [{
      //   date: '1',
      //   name: '王小虎',
      //   address: '上海市普陀区金沙江路 1518 弄'
      // }],
      showDialog: false,
      tagsss: {}
    }
  },
  components: {
    tagsAdd
  },
  created () {
    this.tagS()
    // this.updateApi()
  },
  methods: {
    // 搜索功能
    sousuo () {
      // console.log(this.formInline.directorryName1)
      if (this.formInline.region === '') {
        this.tagS(this.formInline.directorryName1)
      } else { this.tagS(this.formInline.directorryName1, this.formInline.region) }
    },
    addEmployeeFn () {
      this.showDialog = true
    },
    // 请求列表
    async tagS (a, b) {
      this.isLoading = true
      const { data } = await list({ page: this.page, pagesize: this.pagesize, tagName: a, state: b })
      // console.log(data)
      this.employeeList = data.items
      this.counts = data.counts
      this.isLoading = false
    },
    // 删除
    async outApi (row) {
      this.$confirm('确认删除？', '温馨提示').then(async () => {
        await remove(row)
        this.$message.success('删除成功')// 提示
        if (this.employeeList.length === 1 && this.page > 1) {
          this.page--
        }
        this.tagS() // 再获取接口
      }).catch(() => {})
    },
    async updateApi (val) {
      this.showDialog = true
      this.tagsss = val
      // const res = await update({ subjectID: val })
      // console.log(res)
    },
    // onSubmit () {
    //   console.log('submit!')
    // },
    // 页容量改变
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.pagesize = val
      if (this.formInline.region === '') {
        this.tagS(this.formInline.directorryName1)
      } else { this.tagS(this.formInline.directorryName1, this.formInline.region) }
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.page = val
      if (this.formInline.region === '') {
        this.tagS(this.formInline.directorryName1)
      } else { this.tagS(this.formInline.directorryName1, this.formInline.region) }
    },
    // 序号排列
    indexMethod (index) {
      return (this.page - 1) * this.pagesize + index + 1
    },
    formatime (time) {
      return parseTime(time)
    },
    // 清除输入框
    resetInput () {
      this.formInline.directorryName1 = ''
      this.state = null
      this.formInline.region = ''
    },
    async editState (id, state) {
      await changeState({ id, state: state ? 0 : 1 })
      // console.log(row)
      this.$message.success('切换成功')
      this.tagS()
    }
  }
}
</script>

<style scoped lang="scss">
.card{
  ::v-deep .el-table th {
background-color: #fafafa;
border-bottom: 2px solid #e8e8e8 !important;
 }
  margin: 10px;
  //左侧背景
  .bg-purple {
    background: #fff;
  }
  // 右侧背景
  .bg-purple-light {
    background: #fff;
  }
  .grid-content {
    border-radius: 10px;
    min-height: 40px;
  }
  .alert{
    margin-bottom: 15px;
  }
  .ones{
  background: #fff;
  }
  // 添加按钮
  .add{
    float: right;
  }
  // 标签名和状态名
  .bqname{
    margin-left:10px ;
  }
  .ztname{
     margin-left:30px ;
  }
  .pagination{
    // float: left;
    margin-top:15px ;
    display: flex;
    justify-content: right;
  }
}

</style>
